package cn.kgc.ssm.service.impl;

import cn.hutool.crypto.digest.MD5;
import cn.kgc.ssm.commons.ResponseResult;
import cn.kgc.ssm.entity.Member;
import cn.kgc.ssm.mapper.MemberMapper;
import cn.kgc.ssm.service.MemberService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/5/16
 * @description
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberMapper memberMapper;

    @Override
    public ResponseResult memberPage(Integer page, Integer limit, String memberName) {

        // 开启分页
        PageHelper.startPage(page, limit);
        //  条件查询
        List<Member> members = memberMapper.searchMembers(memberName);

        PageInfo<Member> memberPageInfo = new PageInfo<>(members);
        long total = memberPageInfo.getTotal();


        return ResponseResult.success().count(total).data(members);
    }

    @Override
    public ResponseResult addMember(Member member) {

        try {
            // 会员的默认密码
            String md5Password = MD5.create().digestHex16("111111");
            member.setPassword(md5Password);

            memberMapper.insertMember(member);
        } catch (DuplicateKeyException e) {   // DuplicateKeyException     DataIntegrityViolationException
            e.printStackTrace();
            return ResponseResult.fail().message("会员名已存在");
        } catch (DataIntegrityViolationException e) {
            e.printStackTrace();
            return ResponseResult.fail().message("身份证号错误");
        }
        return ResponseResult.success().message("会员添加成功");
    }

    @Override
    public ResponseResult getMember(Integer id) {

        Member member = memberMapper.selectById(id);
        return ResponseResult.success().data(member);
    }

    @Override
    public ResponseResult editMember(Member member) {

        try {
            memberMapper.updateMember(member);
        } catch (DataIntegrityViolationException e) {
            e.printStackTrace();
            return ResponseResult.fail().message("身份证号错误");
        }
        return ResponseResult.success().message("更新成功");
    }

    @Override
    public ResponseResult removeMember(Integer[] ids) {

        memberMapper.deleteMembers(ids);
        return ResponseResult.success().message("删除成功");
    }

    @Override
    public ResponseResult resetPassword(Integer id) {

        String password = MD5.create().digestHex16("111111");

        memberMapper.updatePassword(id, password);
        return ResponseResult.success().message("密码更新成功");
    }

    @Override
    public ResponseResult registerMember(Member member) {

        try {

            String md5Password = MD5.create().digestHex16(member.getPassword());
            member.setPassword(md5Password);
            memberMapper.insertMember(member);

        } catch (DuplicateKeyException e) {   // DuplicateKeyException
            e.printStackTrace();
            return ResponseResult.fail().message("会员名已存在");
        }

        return ResponseResult.success().message("会员添加成功");

    }

    @Override
    public ResponseResult login(Member member, HttpServletRequest request) {

        String md5Password = MD5.create().digestHex16(member.getPassword());
        member.setPassword(md5Password);

        Member member1 = memberMapper.selectByMemberNameAndPassword(member);
        if (ObjectUtils.isEmpty(member1)){
            return  ResponseResult.fail().message("会员名会密码错误");
        }

        // 会员信息的存储
        HttpSession session = request.getSession();
        session.setAttribute("member",member1);

        return ResponseResult.success().message("认证通过");
    }
}
